% # variable:
% # book, eroot, bookuuid, target_uuid, max_level, trace_path_enable

% import datetime as DT
% from lonote.civa.tz import local_timezone

%def get_class(enote, target_uuid): 
    %cls = ''
    %if enote.get('uuid') == target_uuid:
        %cls = 'target'
    %elif enote.find('.//note[@uuid="{}"]'.format(target_uuid)) is not None:
        %cls = 'path'
    %end
    %return cls
%end

%def get_title(enote):
    %title = enote.findtext('./title')
    %return title
%end

%def get_path(enote, bookuuid):
    %path = '/site/{bookuuid}/{uuid}/'.format(bookuuid = bookuuid, uuid = enote.get('uuid'))
    %return path
%end

%def get_tail(book, enote):
    %tail = '<span class="notify-small">'

    %allnotes = enote.findall('./note')
    %if len(allnotes) > 0:
        %tail += ' <img class="icon" src="/static/icon/sidebar/folder.png">'
    %end

    % resource_list = book.resource_list(enote.get('uuid'))

    % if len(resource_list) > 0:
        % for res in resource_list:
            % if res['res_type'] == 'att':
                % tail += ' <img class="icon" src="/static/icon/sidebar/att.png">'
                % break
            % end
        % end
        % for res in resource_list:
            % if res['res_type'] == 'emb':
                % tail += ' <img class="icon" src="/static/icon/sidebar/emb.png">'
                % break
            % end
        % end
    % end

    %tail += '</span>'
    %return tail
%end

% now = DT.datetime.now(tz = local_timezone())

% time_format = '%Y-%m-%d'
% im_time_format = '%Y-%m-%dT%H:%M:%S.%f%z'

% def get_mtime(enote):
    % mtime = DT.datetime.strptime(enote.findtext('modified_time'), im_time_format)
    % desc = ''
    % delta = now - mtime

    % if delta.days == 0:
        % desc = ' (Today)'
    % elif delta.days == 1:
        % desc = ' (Yesterday)'
    % elif 2 <= delta.days <= 29:
        % desc = ' ({} days ago)'.format(delta.days)
    % elif 30 <= delta.days <= 364:
        % desc = ' ({} months ago)'.format(delta.days // 30)
    % else:
        % desc = ' ({} years ago)'.format(delta.days // 365)
    % end

    % return mtime.strftime(time_format) + desc
% end


% def multi_level_menu(enote, target_uuid, level):
    % # 依據給定的enote，產生一定數量層級的menu 最小值是0
    % def recu(enote, target_uuid, level):
        % if level < 0:
            % return
        % end

        % title = get_title(enote)

        % if trace_path_enable:
            <li><a class="{{get_class(enote, target_uuid)}}" title="Modified Time: {{get_mtime(enote)}}" href="{{get_path(enote, bookuuid)}}">{{title}}{{!get_tail(book, enote)}}</a>
        % else:
            <li><a title="{{title}}" href="{{get_path(enote, bookuuid)}}">{{get_title(enote)}}{{!get_tail(book, enote)}}</a>
        % end

        %esubnotes = enote.findall('./note');
        %level = level -1
        %if len(esubnotes) and not (level < 0) :
            <ul>
                %for esub in esubnotes:
                    %recu(esub, target_uuid, level)
                %end
            </ul>
        %end
        </li>
    %end

    %recu(enote, target_uuid, level)
%end

%multi_level_menu(eroot, target_uuid, max_level)
